package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.listener.SubjectExcelListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.pojo.EduSubject;
import com.atguigu.eduservice.pojo.excel.SubjectData;
import com.atguigu.eduservice.pojo.subject.OneSubject;
import com.atguigu.eduservice.pojo.subject.TwoSubject;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-11-19
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {
    //添加课程分类
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService eduSubjectService) {
        try {
            //文件输入流
            InputStream in = file.getInputStream();
            //调用方法进行读取
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //1.查询所有的一级分类
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");
        //查询
        List<EduSubject> oneSubjectsLists = baseMapper.selectList(wrapperOne);

        //2.查询出所有的二级分类
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id","0");
        //查询显示
        List<EduSubject> TwoSubjectLists = baseMapper.selectList(wrapperTwo);

        //创建list结合存储最终封装的数据
        List<OneSubject> finalSubjectList = new ArrayList<>();

        //3.封装一级分类
        //把查询出来的所有的一级分类的所有集合进行遍历
        for (EduSubject EduSubject : oneSubjectsLists) {
            //得到每个对象

            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(EduSubject,oneSubject);

            finalSubjectList.add(oneSubject);
            //创建list集合，封装每个一级分类的二级分类
            List<TwoSubject> twoSubjects = new ArrayList<>();
            //遍历二级分类集合
            for (EduSubject TwoEduSubject : TwoSubjectLists) {
                //获取每个二级分类
                String parentId = TwoEduSubject.getParentId();
                if (parentId.equals(EduSubject.getId())){
                    TwoSubject subject = new TwoSubject();
                    BeanUtils.copyProperties(TwoEduSubject,subject);
                    twoSubjects.add(subject);
                }

            }
            //二级放入一级中
            oneSubject.setChildren(twoSubjects);
        }
        return finalSubjectList;
    }

}
